/**
 * Created by Zfy on 2015/11/3.
 */

var init_val=1;
function updateStatus(type,id,state){
    if(type=="lock"){
        if(state==1){
            $("#biaoti2").html("锁定用户,用户状态将不可用！禁止登录后台");
        }else{
            $("#biaoti2").html("解锁用户,用户状态恢复！");
        }
    }else{
        $("#biaoti2").html("重置用户密码！重置密码为：123456");
    }
    $("#updateUser").modal();
    $("#update_click").click(function(){
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"id":id,"type":type},
            url:"/index.php?r=rbac/checkuser",
            success:function(data) {
                if(data==true){
                    location.reload();
                }
            }
        });
    });
}
function addUser(){
    $("#error_check").empty();
    $("#user_error").empty();
    $("#biaoti").html("新增用户");
    $("#user_id").val('');
    $("#newpassword").show();
    $("#newpassword").val("");
    $("#truename").val('');
    $("#user_name").val('');
    $("#mobile").val('');
    $("#position").val('');
    $("#areaFrom").attr("action","/index.php?r=rbac/adduser");
    $("input[name=sex][value="+init_val+"]").attr('checked','true');
    $("input[name=level][value="+init_val+"]").attr('checked','true');
    $("#province_id").find("option[value='']").attr("selected",true);
    $("#city_id").empty();
    $("#house_id").empty();
    $("#city_id").append("<option value=''>--城市--</option>");
    $("#house_id").append("<option value=''>--楼盘--</option>");
    getRole(init_val);
    $("#addUser").modal();
    $("#area_click").click(function(){
        $("#error_check").empty();
        var password=$("#password").val();
        if(password==''||password==null){
            $("#error_check").append("<p style='color: red'>密码不能为空</p>");
            return false;
        }
        addUserSubmit("add");
    });
}
function addUserSubmit(type) {
    $("#error_check").empty();
    var user_name=$("#user_name").val();
    var pattern=/[`~!@#\$%\^\&\*\(\)_\+<>\?:"\{\},\.\\\/;'\[\]]/im;
    if(user_name==''||user_name==null||pattern.test(user_name)){
        $("#error_check").append("<p style='color: red'>用户名不能为空或不能有特殊字符</p>");
        return false;
    }
    var truename=$("#truename").val();
    if(truename==''||truename==null){
        $("#error_check").append("<p style='color: red'>姓名不能为空</p>");
        return false;
    }
    var mobile=$("#mobile").val();
    var mobilett =/^1[0-9][0-9]\d{4,8}$/;
    if(mobile==''||mobile==null||!mobilett.test(mobile)){
        $("#error_check").append("<p style='color: red'>手机号码格式不正确</p>");
        return false;
    }
    var level=$('input:Radio[name="level"]:checked').val();
    if(level==1){
        var city_id=$("#city_id").val();
        if(city_id==""){
            $("#error_check").append("<p style='color: red'>请选择一个城市</p>");
            return false;
        }
    }
    if(level==2){
        var house_id=$("#house_id").val();
        if(house_id==""){
            $("#error_check").append("<p style='color: red'>请选择一个楼盘</p>");
            return false;
        }
    }
    var user_role=$('#user_role').val();
    if(user_role==null||user_role==""){
        $("#error_check").append("<p style='color: red'>请配置一个位角色</p>");
        return false;
    }
    if(type=="add"){
        $.ajax({
            type:"POST",
            dataType:"json",
            data:{"user_name":user_name},
            url:"/index.php?r=rbac/checkusername",
            success:function(data) {
                if(data==true){
                    $("#areaFrom").submit();
                }else{
                    $("#error_check").append("<p style='color: red'>用户名不可重复</p>");
                    return false;
                }
            }
        });
    }else{
        $("#areaFrom").submit();
    }


}
function updateUser(id){
    $("#biaoti").html("编辑用户");
    $("#areaFrom").attr("action","/index.php?r=rbac/updateuser");
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"id":id},
        url:"/index.php?r=rbac/getuser",
        success:function(data) {
            var data=eval(data);
            $("#user_id").val(data['id']);
            $("#newpassword").hide();
            $("#truename").val(data['truename']);
            $("#user_name").val(data['username']);
            $("#mobile").val(data['mobile']);
            $("#position").val(data['position']);
            $("input[name=sex][value='"+data['sex']+"']").attr('checked','true');
            $("input[name=level][value='"+data['level']+"']").attr('checked','true');
            getRole(data['level'],data['role']['item_name']);
            $("#province_id").find("option[value='"+data['area_id']+"']").attr("selected",true);
            getNextcity(data['area_id'],data['city_id']);
            getNextHouse(data['city_id'],data['house_id']);
            $("#addUser").modal();
            $("#area_click").click(function(){
                $("#error_check").empty();
                addUserSubmit();
            });
        }
    });
}
function getRole(level,item_name){
    if(level==1){
        $("#province_id").show();
        $("#city_id").show();
         $("#house_id").hide();
    }else if(level==2){
        $("#province_id").show();
        $("#city_id").show();
        $("#house_id").show();
    }else{
        $("#province_id").hide();
        $("#city_id").hide();
        $("#house_id").hide();
    }

    $("#user_role").empty();
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"level":level},
        url:"/index.php?r=rbac/getlevelrole",
        success:function(data) {
            if(data!=false){
                var data=eval(data);
                $("#user_role").append("<option value=''>选择角色</option>");
                for(var i=0;i<data.length;i++){
                    $("#user_role").append("<option value=\'"+data[i]['name']+"\'>"+data[i]['description']+"</option>");
                }
            }else{
                return false;
            }
            $("#user_role").find("option[value='"+item_name+"']").attr("selected",true);
        }
    });
}
function getNextcity(id,city_id){
    if(id==''){
        $("#city_id").empty();
        $("#city_id").append(" <option value=''>--城市--</option>");
        return false;
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{'id':id},
        url:"/index.php?r=rbac/getnextarea",
        success:function(data) {
            var data = eval(data);
            $("#city_id").empty();
            $("#city_id").append(" <option value=''>--城市--</option>");
            for (var i = 0; i < data.length; i++) {
                $("#city_id").append(" <option value='" + data[i]['id'] + "'>" + data[i]['name'] + "</option>");
            }
            $("#city_id").find("option[value='"+city_id+"']").attr("selected",true);
        }
    });
}
function getNextHouse(id,house_id){
    if(id==''){
        $("#house_id").empty();
        $("#house_id").append(" <option value=''>--楼盘--</option>");
        return false;
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"id":id},
        url:"/index.php?r=rbac/getnexthouse",
        success:function(data) {
            var data=eval(data);
                $("#house_id").empty();
                $("#house_id").append("<option value=''>--楼盘--</option>");
                for (var i = 0; i < data.length; i++) {
                    $("#house_id").append(" <option value='" + data[i]['id'] + "'>" + data[i]['house_name'] + "</option>");
                }
            $("#house_id").find("option[value='"+house_id+"']").attr("selected",true);
           }
    });
}
function getNextArea(level,id,zid){
    $.ajax({
        type:"POST",
        dataType:"json",
        data:{"level":level,"id":id},
        url:"/index.php?r=rbac/getnextarea",
        success:function(data) {
            var data=eval(data);
            if(level==1) {
                $("#house_id").empty();
                $("#house_id").append("<option value=''>--楼盘--</option>");
                for (var i = 0; i < data.length; i++) {
                    $("#city_id").append(" <option value='" + data[i]['id'] + "'>" + data[i]['name'] + "</option>");
                }
                $("#city_id").find("option[value='"+zid+"']").attr("selected",true);
            }else{
                $("#house_id").empty();
                $("#house_id").append("<option value=''>--楼盘--</option>");
                for (var i = 0; i < data.length; i++) {
                    $("#house_id").append(" <option value='"+data[i]['id']+"'>"+data[i]['name']+"</option>");
                }
                $("#house_id").find("option[value='"+zid+"']").attr("selected",true);
            }
        }
    });
}
